package servlet;

import entity.Powerbattery;
import service.PowerbatteryService;
import service.impl.PowerbatteryServletImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/powerbatterySelect")
public class powerBatteryServlet extends HttpServlet {
    PowerbatteryService service = new PowerbatteryServletImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Powerbattery powerbattery = null;
        String opr = request.getParameter("opr");
        if ("all".equals(opr)){
            List<Powerbattery> list = service.powSelect();
            if (list!=null){
                request.setAttribute("list",list);
                request.getRequestDispatcher("powerbatteryIndex.jsp").forward(request,response);
            }
        }
        if("update".equals(opr)){
            powerbattery = new Powerbattery();
            int id =Integer.parseInt(request.getParameter("id"));
            powerbattery = service.selectThis(id);
            request.setAttribute("powerbattery",powerbattery);
            request.getRequestDispatcher("update.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
